<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
    <el-form-item label="SQL片段" prop="sql">
        <el-input
          v-model="queryParams.sql"
          placeholder="请输入SQL片段"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table ref="tables" v-loading="loading" :data="logList" max-height="700">
      <el-table-column
      label="编号"
      type="index"
      align="center"
      width="50">
    </el-table-column>
      <el-table-column
        label="SQL"
        align="center"
        prop="SQL"
        width="400"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="执行数"
        align="center"
        prop="ExecuteCount"
        width="130"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="执行时间"
        align="center"
        prop="TotalTime"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="最慢执行耗时"
        align="center"
        prop="MaxTimespan"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="读取行数"
        align="center"
        prop="FetchRowCount"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="最大并发"
        align="center"
        prop="ConcurrentMax"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row, scope.index)"
            v-hasPermi="['monitor:operlog:query']"
            >详细</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listLog, getSql } from "@/api/druid/sql";

export default {
  name: "Urllog",
  dicts: ["sys_oper_type", "sys_common_status"],
  data() {
    return {
      timeoutId: null, // 用于存储定时器的 ID
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      logList: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "operTime", order: "descending" },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sql: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // 延迟 5 秒后执行一次操作
    this.timeoutId = setInterval(() => {
      this.getList();
    }, 10000);
  },
  beforeDestroy() {
    // 在组件销毁前清除定时器
    if (this.timeoutId) {
      clearInterval(this.timeoutId);
      this.timeoutId = null;
    }
  },
  methods: {
    /** 查询接口调用日志记录列表 */
    getList() {
      this.loading = true;
      getSql(this.queryParams).then((response) => {
        this.logList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 操作日志类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(
        this.dict.type.sys_oper_type,
        row.businessType
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除日志编号为"' + operIds + '"的数据项？')
        .then(function () {
          return delLog(operIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal
        .confirm("是否确认清空所有操作日志数据项？")
        .then(function () {
          return cleanOperlog();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "monitor/callLog/export",
        {
          ...this.queryParams,
        },
        `接口调用日志_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.el-table .el-table__row {
  height: 40px; /* 根据需要调整高度 */
}
</style>
